from django.shortcuts import render
from django.views.decorators.http import *
from django.http import HttpResponse
from django.views.generic import View
from django.conf import settings
from django.http import HttpResponseRedirect
from kkoffer.user.models import article
from django.conf import settings
from django.http import JsonResponse
from .forms import articleForm
from kkoffer.tool.views import login_required
import requests as req
from pyquery import PyQuery as py
import re
import hashlib
import sh

def articleList(request,id=''):
    context = {}
    obj = article.objects
    if id:
        data = obj.filter(type=id)
    else:
        data = obj.filter()
    context = {'list':data}
    context['id'] = ""
    if id:
        context['id'] = int(id)
    context['type'] = article._meta.get_field('type').choices
    # import ipdb; ipdb.set_trace()
    return render(request,'article.html',context)


def articleDetails(request,id):
    context = {}
    context = article.objects.filter(pk=id).values()[0]
    # import ipdb; ipdb.set_trace()
    return render(request,'articleDetails.html',context)

class publish(View):
    def get(self,request):
        context = {}
        context['type'] = article._meta.get_field('type').choices
        return render(request,'publish.html',context)
    def post(self,request):
        post = request.POST.copy()
        post['uid'] = request.session['id']
        if request.POST.get('id'):
            obj = article.objects.get(pk = request.POST.get('id'))
            f = articleForm(post, instance=obj)
        else:
            f = articleForm(post)
        # if f.is_valid():
        # import ipdb; ipdb.set_trace()
        f.save()
        return HttpResponseRedirect('/article/manage')

class broadcast(View):
    def get(self,request):
        context = {}
        return render(request,'broadcast.html')
    def post(self,request):
        post = request.POST.copy()
        post['uid'] = request.session['id']
        f = articleForm(post)
        f.save()
        return HttpResponseRedirect('/article/manage')


def articleManage(request):
    context = {}
    context['list'] = article.objects.filter()
    return render(request,'articleManage.html',context)

def articleEdit(request,id):
    context = {}
    context = article.objects.filter(pk=id).values()[0]
    context['articleType'] = context['type']
    context['type'] = article._meta.get_field('type').choices
    return render(request,'publish.html',context)

def articleDel(request,id):
    context = {}
    article.objects.get(pk=id).delete()
    return HttpResponseRedirect('/article/manage')

class collect(View):
    def get(self,request):
        context = {}
        context['type'] = article._meta.get_field('type').choices
        return render(request,'collectUrl.html',context)
    def post(self,request):
        context = {}
        url = request.POST["url"]
        type = request.POST["type"]
        sh('python',)
        obj = article(title=title,content=content,type=type,uid_id=request.session['id'])
        obj.save()
        # return render(request,'publish.html',context)
        return HttpResponseRedirect('/article/manage/')









